package com.sunday.pinxinghui.model.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.Constants;
import com.sunday.common.utils.StringUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.ClearEditText;
import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.base.BaseApplication;
import com.sunday.pinxinghui.entity.Address;
import com.sunday.pinxinghui.entity.Province;
import com.sunday.pinxinghui.network.ApiClient;
import com.sunday.pinxinghui.widgets.SelectCityWindow;
import com.sunday.pinxinghui.widgets.SelectStreetWindow;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.sunday.pinxinghui.R.id.rightTxt;

/**
 * Created by 刘涛 on 2017/8/11.
 */

public class AddAddressActivity extends BaseActivity {


    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.tv_name)
    ClearEditText tvName;
    @Bind(R.id.textView)
    TextView textView;
    @Bind(R.id.tv_phone)
    ClearEditText tvPhone;
    @Bind(R.id.tv_place)
    TextView tvPlace;
    @Bind(R.id.tv_street)
    TextView tvStreet;
    @Bind(R.id.tv_finalAddress)
    ClearEditText tvFinalAddress;
    @Bind(R.id.btn_submit)
    TextView btnSubmit;

    private Address address;
    private long memberId;
    private Integer provinceId, cityId, districtId, streetId, addressId = null;
    private SelectCityWindow selectCityWindow;
    private SelectStreetWindow selectStreetWindow;
    private InputMethodManager imm;
    private boolean isSelectMode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_addr);
        ButterKnife.bind(this);
        address = (Address) getIntent().getSerializableExtra("address");
        initView();
    }

    private void initView() {
        memberId = BaseApplication.getInstance().getMember().getId();
        Address address = (Address) getIntent().getSerializableExtra("address");
        isSelectMode = getIntent().getBooleanExtra("isSelectMode", false);
        //tvName.setText(BaseApplication.getInstance().getMember().getNickName());
        if (address == null) {
            titleView.setText("添加地址");
        } else {
            titleView.setText("编辑地址");
            tvName.setText(address.getName());
            tvPlace.setText(address.getProvinceName() + address.getCityName() + address.getDistrictName());
            tvStreet.setText(address.getStreetName());
            tvPhone.setText(address.getMobile());
            tvFinalAddress.setText(address.getAddress());
            provinceId = address.getProvinceId();
            cityId = address.getDistrictId();
            districtId = address.getCityId();
            streetId = address.getStreetId();
            addressId = address.getId();
        }

        imm = (InputMethodManager)
                getSystemService(Context.INPUT_METHOD_SERVICE);


    }

    @OnClick({R.id.tv_place, R.id.tv_street, R.id.btn_submit})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_place:
                if (selectCityWindow == null) {
                    selectCityWindow = new SelectCityWindow(mContext);
                }
                selectCityWindow.showPopupWindow(tvPlace);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                selectCityWindow.setOnSelectListener(onCitySelectListener);
                break;
            case R.id.tv_street:
                if (provinceId == null) {
                    ToastUtils.showToast(mContext, "请先选择省市区");
                    return;
                }
                if (selectStreetWindow == null) {
                    selectStreetWindow = new SelectStreetWindow(mContext, districtId);
                } else {
                    selectStreetWindow.setFatherId(districtId);
                }
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                selectStreetWindow.showPopupWindow(tvStreet);
                selectStreetWindow.setOnSelectListener(onCitySelectListener1);
                break;
            case R.id.btn_submit:
                String name = tvName.getText().toString().trim();
                String mobile = tvPhone.getText().toString().trim();
                String addrDetail = tvFinalAddress.getText().toString().trim();
                String street = tvStreet.getText().toString().trim();
                String provice = tvPlace.getText().toString().trim();
                if (TextUtils.isEmpty(name)) {
                    ToastUtils.showToast(mContext, "请填写收货人姓名");
                    return;
                }
                if (TextUtils.isEmpty(addrDetail)) {
                    ToastUtils.showToast(mContext, "请填写具体收获位置");
                    return;
                }
                if (TextUtils.isEmpty(mobile) || !StringUtils.isMobileNO(mobile)) {
                    ToastUtils.showToast(mContext, "请填写正确手机号");
                    return;
                }
                if (TextUtils.isEmpty(provice)) {
                    ToastUtils.showToast(mContext, "请选择地区");
                    return;
                }

                if (TextUtils.isEmpty(street)) {
                    ToastUtils.showToast(mContext, "请选择街道");
                    return;
                }

                showLoadingDialog(0);
                Call<ResultDO<Address>> call = ApiClient.getApiAdapter().saveAddr(memberId, provinceId, cityId, districtId, streetId,
                        addrDetail, name, mobile, addressId);
                call.enqueue(new Callback<ResultDO<Address>>() {
                    @Override
                    public void onResponse(Call<ResultDO<Address>> call, Response<ResultDO<Address>> response) {
                        dissMissDialog();
                        if (isFinish || response.body() == null) {
                            return;
                        }
                        if (response.body().getCode() == 0) {
                            if (isSelectMode && response.body().getResult() != null) {
                                Intent data = new Intent();
                                data.putExtra("address", response.body().getResult());
                                setResult(RESULT_OK, data);
                            } else {
                                ToastUtils.showToast(mContext, "保存成功");
                            }
                            finish();
                        }
                    }

                    @Override
                    public void onFailure(Call<ResultDO<Address>> call, Throwable t) {
                        dissMissDialog();
                    }
                });

                break;
        }
    }

    SelectCityWindow.OnCitySelectListener onCitySelectListener = new SelectCityWindow.OnCitySelectListener() {
        @Override
        public void onSelect(Province province, Province city, Province district) {
            provinceId = province.getId();
            cityId = city.getId();
            districtId = district.getId();
            tvPlace.setText(province.getName() + city.getName() + district.getName());
        }
    };

    SelectStreetWindow.OnCitySelectListener onCitySelectListener1 = new SelectStreetWindow.OnCitySelectListener() {
        @Override
        public void onSelect(Province street) {
            streetId = street.getId();
            tvStreet.setText(street.getName());
        }
    };


}
